﻿// For an introduction to the Blank template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232509
(function () {
    "use strict";

    var app = WinJS.Application;
    var activation = Windows.ApplicationModel.Activation;

    var canvas, context;
    var gameStage;
    var preloadQ;
    var logoScreenImage, backgroundImage;
    var logoScreenBitmap, backgroundBitmap;
    var shipImage, tankImage, airplanImage, soldierImage;
    var shipBitmap, tankBitmap, airplanBitmap, soldierBitmap;
    var shipEnemyImage, tankEnemyImage, airplanEnemyImage, soldierEnemyImage;
    var shipEnemyBitmap, tankEnemyBitmap, airplanEnemyBitmap, soldierEnemyBitmap;
    var startbt_1Image, startbt_2Image;
    var startbt_1Bitmap, startbt_2Bitmap;
    var soldier, airplan, tank, ship;
    var soldierEnemy, airplanEnemy, tankEnemy, shipEnemy;
    var player;
    var greatBase;
    var scaleX = window.innerWidth / 1920;
    var scaleY = window.innerHeight / 1080;
    var newGame = true;
    var battle = false;
    var unit = new Array();
    var unitEnemy = new Array();
    var memposition = new Array();
    var mempositionEnemy = new Array();
    var memPositionBattle = new Array();
    var memPositionEnemyBattle = new Array();
    var position = [];
    var Battle = new Array();
    var BattleEnemy = new Array();
    var count_click = 0;
    var move = 303;
    var max, maxEnemy, sum_max;
    var numShoot;
    var remove = new Array();
    var removeEnemy = new Array();
    var index;
    var timeText, min = 0, sec = 0, timeCount = 0;
    var statusText, msg;
    var pointText;

    app.onactivated = function (args) {
        if (args.detail.kind === activation.ActivationKind.launch) {
            if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
                // TODO: This application has been newly launched. Initialize
                // your application here.
            } else {
                // TODO: This application has been reactivated from suspension.
                // Restore application state here.
            }
            args.setPromise(WinJS.UI.processAll());
        }
    };

    app.oncheckpoint = function (args) {
        // TODO: This application is about to be suspended. Save any state
        // that needs to persist across suspensions here. You might use the
        // WinJS.Application.sessionState object, which is automatically
        // saved and restored across suspension. If you need to complete an
        // asynchronous operation before your application is suspended, call
        // args.setPromise().
    };

    document.addEventListener("DOMContentLoaded", initialize, false);

    app.start();

    function Soldier(gfx) {
        this.resource = 100;
        this.rate_fire;
        this.damage = 1;
        this.range = 250;
        this.mobility = 0.08;
        this.health;
        this.elusiveness = 8;
        this.accuracy = 2;
        this.onShoot = false;
        this.choose = true;
        this.bounty = 1;
        this.bitmap = gfx;
        this.positionX;
        this.myshoot;
        this.healthText;
        this.assign_shoot = function (unt) {
            this.myShoot = window.setTimeout(function () { unt.onShoot = true; }, 3000);
        }
        this.assign_health = function () {
            this.health = this.damage * this.resource;
            this.healthText = new createjs.Text(this.health, "15px sans-serif", "blue");
            this.healthText.x = this.bitmap.x;
            this.healthText.y = this.bitmap.y;
            gameStage.addChild(this.healthText);
        }
        this.shooting = function (unt) {
            var numShooted;
            var dead;
            //numShooted = this.resource * this.damage + this.resource * Math.cos((this.elusiveness / accuracy) % (Math.PI / 2) + (Math.PI / 2));
            numShooted = (unt.resource * unt.damage) / this.elusiveness;
            return numShooted;
        }
    }

    function Tank(gfx) {
        this.resource = 100;
        this.rate_fire;
        this.damage = 3;
        this.range = 350;
        this.mobility = 0.1;
        this.health;
        this.elusiveness = 5;
        this.accuracy = 5;
        this.onShoot = false;
        this.choose = true;
        this.bounty = 2;
        this.positionX;
        this.bitmap = gfx;
        this.myShoot
        this.assign_shoot = function (unt) {
            this.myShoot = window.setTimeout(function () { unt.onShoot = true; }, 5000);
        }
        this.assign_health = function () {
            this.health = this.damage * this.resource;
            this.healthText = new createjs.Text(this.health, "15px sans-serif", "blue");
            this.healthText.x = this.bitmap.x;
            this.healthText.y = this.bitmap.y;
            gameStage.addChild(this.healthText);
        }
        this.shooting = function (unt) {
            var numShooted;
            var dead;
            //numShooted = this.resource * this.damage + this.resource * Math.cos((this.elusiveness / accuracy) % (Math.PI / 2) + (Math.PI / 2));
            numShooted = (unt.resource * unt.damage) / this.elusiveness;
            return numShooted;
        }
    }

    function Airplan(gfx) {
        this.resource = 100;
        this.rate_fire;
        this.damage;
        this.range = 600;
        this.mobility;
        this.health;
        this.elusiveness;
        this.onShoot = false;
        this.choose = true;
        this.bounty = 150;
        this.positionX;
        this.bitmap = gfx;
        this.assign_shoot = function () {
            window.setTimeout(function () { this.onShoot = true; }, 90000);
        }
        this.assign_health = function () {
            this.health = this.damage * this.resource;
        }
        /*this.shooting = function () {
            onShoot = true;
        }*/
    }

    function Ship(gfx) {
        this.resource = 100;
        this.rate_fire;
        this.damage;
        this.range = 500;
        this.mobility;
        this.health;
        this.elusiveness;
        this.onShoot = false;
        this.choose = true;
        this.bounty = 100;
        this.positionX;
        this.bitmap = gfx;
        this.assign_shoot = function () {
            window.setTimeout(function () { this.onShoot = true; }, 75000);
        }
        this.assign_health = function () {
            this.health = this.damage * this.resource;
        }
        /*this.shooting = function () {
            onShoot = true;
        }*/
    }

    function GreatBase() {
        this.level = 1;
        this.cost = 5000;
        this.produce = false;
        this.timeout;
        this.produceUnit = function () {
            this.timeout = window.setTimeout(function () { greatBase.produce = true; }, 2000);
        }
        this.produce_permis = function () {
            if (this.produce) this.produce = false;
            else {
                this.produce = true;
            }
        }
        this.upgrade = function () {
            if (player.point >= this.cost) {
                cost = cost * 2;
                this.level += 1;
                if (this.level == 5) {
                    tankImage = preloadQ.getResult("tank");
                    tank = new Tank(new createjs.Bitmap(tankImage));
                    unit[1] = tank;
                    position = memposition[1].split(",");
                    unit[1].bitmap.x = position[0];
                    unit[1].bitmap.y = position[1];
                    unit[1].positionX = 303;
                    gameStage.addChild(unit[1]);

                    tankEnemyImage = preloadQ.getResult("tank");
                    tankEnemy = new Tank(new createjs.Bitmap(tankEnemyImage));
                    unitEnemy[1] = tankEnemy;
                    position = mempositionEnemy[1].split(",");
                    unitEnemy[1].bitmap.x = position[0];
                    unitEnemy[1].bitmap.y = position[1];
                    unitEnemy[1].positionX = 991;
                    gameStage.addChild(unit[1]);
                }
                else if (this.level == 10) {
                    shipImage = preloadQ.getResult("ship");
                    ship = new Ship(new createjs.Bitmap(shipImage));
                    unit[2] = ship;
                    position = memposition[2].split(",");
                    unit[2].bitmap.x = position[0];
                    unit[2].bitmap.y = position[1];
                    unit[2].positionX = 303;
                    gameStage.addChild(unit[2]);

                    shipEnemyImage = preloadQ.getResult("ship");
                    shipEnemy = new Ship(new createjs.Bitmap(tankEnemyImage));
                    unitEnemy[2] = shipEnemy;
                    position = mempositionEnemy[2].split(",");
                    unitEnemy[2].bitmap.x = position[0];
                    unitEnemy[2].bitmap.y = position[1];
                    unitEnemy[2].positionX = 991;
                    gameStage.addChild(unit[2]);
                }
                else if (this.level == 15) {
                    airplanImage = preloadQ.getResult("airplan");
                    airplan = new Airplan(new createjs.Bitmap(airplanImage));
                    unit[3] = airplane;
                    position = memposition[3].split(",");
                    unit[3].bitmap.x = position[0];
                    unit[3].bitmap.y = position[1];
                    unit[3].positionX = 303;
                    gameStage.addChild(unit[3]);

                    airplanEnemyImage = preloadQ.getResult("airplan");
                    airplanEnemy = new Airplan(new createjs.Bitmap(tankEnemyImage));
                    unitEnemy[3] = airplanEnemy;
                    position = mempositionEnemy[3].split(",");
                    unitEnemy[3].bitmap.x = position[0];
                    unitEnemy[3].bitmap.y = position[1];
                    unitEnemy[3].positionX = 991;
                    gameStage.addChild(unit[3]);
                }

            }
        }
    }

    function Player() {
        this.point = 0;
    }

    function initialize() {
        canvas = document.getElementById("gameCanvas");
        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;
        context = canvas.getContext("2d");
        gameStage = new createjs.Stage(canvas);

        canvas.addEventListener("MSPointerUp", pointerUp, false);
        canvas.addEventListener("MSPointerMove", clicked);
        loadContent();

    }

    function clicked(e) {
        document.getElementById("X").innerHTML = e.pageX;
        document.getElementById("Y").innerHTML = e.pageY;
    }

    function loadContent() {
        preloadQ = new createjs.LoadQueue();   //????????????????????????????????? ?????????????????????????????????????
        preloadQ.on("complete", prepareStage, this);  //preloadQ ??????????????? prepareStage
        // complete ????????????????????????????????????????????????????????????(prepareStage)

        preloadQ.loadManifest([  //??????????????????????????????????? ???????????????????
            { id: "logoScreen", src: "images/logoScreen.png" },
            { id: "background", src: "images/background.png" },
            { id: "ship", src: "images/ship.png" },
            { id: "tank", src: "images/tank.png" },
            { id: "helicopter", src: "images/helicopter.png" },
            { id: "airplan", src: "images/airplan.png" },
            { id: "startbt_1", src: "images/buttom1.png" },
            { id: "startbt_2", src: "images/buttom2.png" }
        ]);

    }

    function prepareStage() {
        logoScreenImage = preloadQ.getResult("logoScreen");
        logoScreenBitmap = new createjs.Bitmap(logoScreenImage);  //??????????????????? ??????? bitmap (????????????)
        logoScreenBitmap.scaleX = scaleX;
        logoScreenBitmap.scaleY = scaleY;
        gameStage.addChild(logoScreenBitmap);

        backgroundImage = preloadQ.getResult("background");
        backgroundBitmap = new createjs.Bitmap(backgroundImage);  //??????????????????? ??????? bitmap (????????????)
        backgroundBitmap.x = window.innerWidth / 2 - backgroundBitmap.image.width / 2;
        backgroundBitmap.y = window.innerHeight / 2 - backgroundBitmap.image.height / 2;
        gameStage.addChild(backgroundBitmap);

        startbt_1Image = preloadQ.getResult("startbt_1");
        startbt_1Bitmap = new createjs.Bitmap(startbt_1Image);  //??????????????????? ??????? bitmap (????????????)
        startbt_1Bitmap.x = 127;
        startbt_1Bitmap.y = 581;
        gameStage.addChild(startbt_1Bitmap);

        startbt_2Image = preloadQ.getResult("startbt_2");
        startbt_2Bitmap = new createjs.Bitmap(startbt_2Image);  //??????????????????? ??????? bitmap (????????????)
        startbt_2Bitmap.x = 127;
        ////////////////////////////////////////////////////
        startbt_2Bitmap.y = 581;
        gameStage.addChild(startbt_2Bitmap);

        var y, x, xEnemy, j, k;
        //assign position in table
        for (j = 0; j < 2; j++) {
            if (j == 0) {
                x = 232; y = 125;
                xEnemy = 1061;
                for (k = 0; k < 8; k++) {
                    memposition[k] = x + "," + y;
                    mempositionEnemy[k] = xEnemy + "," + y;
                    y += 54;
                }
            }
            else {
                x = 158; y = 125;
                xEnemy = 1136;
                for (k = 8; k < 16; k++) {
                    memposition[k] = x + "," + y;
                    mempositionEnemy[k] = xEnemy + "," + y;
                    y += 54;
                }
            }
        }

        //assign position in battle
        x = 303; y = 179; xEnemy = 991;
        for (j = 0; j < 6; j++) {
            memPositionBattle[j] = x + "," + y;
            memPositionEnemyBattle[j] = xEnemy + "," + y;
            y += 54;
        }

        soldierImage = preloadQ.getResult("helicopter");
        soldier = new Soldier(new createjs.Bitmap(soldierImage));
        unit[0] = soldier;
        position = memposition[0].split(",");
        unit[0].bitmap.x = position[0];
        unit[0].bitmap.y = position[1];
        unit[0].positionX = 303;
        gameStage.addChild(unit[0].bitmap);

        tankImage = preloadQ.getResult("tank");
        tank = new Tank(new createjs.Bitmap(tankImage));
        unit[1] = tank;
        position = memposition[1].split(",");
        unit[1].bitmap.x = position[0];
        unit[1].bitmap.y = position[1];
        unit[1].positionX = 303;
        gameStage.addChild(unit[1].bitmap);

        soldierEnemyImage = preloadQ.getResult("helicopter");
        soldierEnemy = new Soldier(new createjs.Bitmap(soldierEnemyImage));
        unitEnemy[0] = soldierEnemy;
        position = mempositionEnemy[0].split(",");
        unitEnemy[0].bitmap.x = position[0];
        unitEnemy[0].bitmap.y = position[1];
        unitEnemy[0].positionX = 991;
        gameStage.addChild(unitEnemy[0].bitmap);

        tankEnemyImage = preloadQ.getResult("tank");
        tankEnemy = new Tank(new createjs.Bitmap(tankEnemyImage));
        unitEnemy[1] = tankEnemy;
        position = mempositionEnemy[1].split(",");
        unitEnemy[1].bitmap.x = position[0];
        unitEnemy[1].bitmap.y = position[1];
        unitEnemy[1].positionX = 991;
        gameStage.addChild(unitEnemy[1].bitmap);

        greatBase = new GreatBase();
        player = new Player();

        timeText = new createjs.Text(min + " : " + sec, "30px sans-serif", "red");
        timeText.x = window.innerWidth / 2 - timeText.getMeasuredWidth() / 2;
        timeText.y = 125;
        gameStage.addChild(timeText);
        timeText.visible = false;

        statusText = new createjs.Text(msg, "40px sans-serif", "red");
        statusText.x = window.innerWidth / 2 - statusText.getMeasuredWidth() / 2;
        statusText.y = 616;
        gameStage.addChild(statusText);
        statusText.visible = false;

        pointText = new createjs.Text("Points : " + player.point, "20px sans-serif", "red");
        pointText.x = 1052;
        pointText.y = 80;
        gameStage.addChild(pointText);
        pointText.visible = false;

        createjs.Ticker.setInterval(window.requestAnimationFrame);
        createjs.Ticker.addEventListener("tick", gameLoop);
    }

    function gameLoop() {
        update();
        draw();
    }

    function update() {
        if (newGame) {
            logoScreenBitmap.visible = true;
            backgroundBitmap.visible = false;
            startbt_1Bitmap.visible = false;
            startbt_2Bitmap.visible = false;
            for (var i = 0; i < unit.length; i++) unit[i].bitmap.visible = false;
            for (var i = 0; i < unitEnemy.length; i++) unitEnemy[i].bitmap.visible = false;
        }
        else {
            logoScreenBitmap.visible = false;
            backgroundBitmap.visible = true;
            startbt_1Bitmap.visible = true;
            pointText.visible = true;
            pointText.text = ("Points : " + player.point);
            for (var i = 0; i < unit.length; i++) unit[i].bitmap.visible = true;
            for (var i = 0; i < unitEnemy.length; i++) unitEnemy[i].bitmap.visible = true;
            if (battle && isEnemy()) {
                time();
                isCollision();
                isRange();
                //document.getElementById("X").innerHTML = unitEnemy[0].health;
                //document.getElementById("Y").innerHTML = unit[0].health;
                if (!isEnemy()) {
                    for (var i = 0; i < unit.length; i++) {
                        position = memposition[i].split(",");
                        unit[i].bitmap.x = position[0];
                        unit[i].bitmap.y = position[1];
                        unit[i].choose = true;
                        unit[i].positionX = 303;
                        unit[i].resource = Math.ceil(unit[i].health / unit[i].damage);
                        gameStage.removeChild(unit[i].healthText);
                    }
                    for (var i = 0; i < unitEnemy.length; i++) {
                        position = mempositionEnemy[i].split(",");
                        unitEnemy[i].bitmap.x = position[0];
                        unitEnemy[i].bitmap.y = position[1];
                        unitEnemy[i].choose = true;
                        unitEnemy[i].positionX = 991;
                        unitEnemy[i].resource = Math.ceil(unitEnemy[i].health / unitEnemy[i].damage);
                        gameStage.removeChild(unitEnemy[i].healthText);
                    }
                    Battle.splice(0, Battle.length);
                    BattleEnemy.splice(0, BattleEnemy.length);
                    battle = false;
                    count_click = 0;
                    greatBase.produceUnit();
                    timeText.visible = false;
                    min = 0; sec = 0; timeCount = 0;
                }
            }
            else
                manufacturing_base();
        }
    }

    function draw() {
        gameStage.update();
    }

    function manufacturing_base() {
        if (greatBase.produce) {

            for (var i = 0; i < unit.length; i++) {
                if (i == 0) {
                    unit[i].resource += 10 * greatBase.level;
                    unitEnemy[i].resource += 10 * greatBase.level * 1.6;
                }
                else if (i == 1) {
                    unit[i].resource += 5 * greatBase.level;
                    unitEnemy[i].resource += 5 * greatBase.level * 1.6;
                }
                else if (i == 2) {
                    unit[i].resource += 3 * greatBase.level;
                    unitEnemy[i].resource += 3 * greatBase.level * 1.6;
                }
                else if (i == 3) {
                    unit[i].resource += 2 * greatBase.level;
                    unitEnemy[i].resource += 2 * greatBase.level * 1.6;
                }
            }

            greatBase.produce = false;
            greatBase.produceUnit();
        }
    }

    function isEnemy() {
        var enemy = true;
        if (min == 1 && sec == 30) {
            showMessage("Time's Up");
            return false;
        }
        if (Battle.length == 0 || BattleEnemy.length == 0) {
            if (Battle.length == 0) showMessage("You Lost");
            else showMessage("You Win");
            return false;
        }
        return enemy;
    }

    function isRange() {
        var rangeShoot;

        for (var i = 0; i < Battle.length; i++)
            for (var j = 0; j < BattleEnemy.length; j++) {
                rangeShoot = Math.abs((Battle[i].bitmap.x + 60) - (BattleEnemy[j].bitmap.x));
                if (rangeShoot <= Battle[i].range) {
                    if (Battle[i].onShoot && Battle[i].health >= 0.1 && BattleEnemy[j].health >= 0.1) {
                        numShoot = BattleEnemy[j].shooting(Battle[i]);
                        if (BattleEnemy[j].health - numShoot >= 0.1) {
                            BattleEnemy[j].health -= numShoot;
                            BattleEnemy[j].resource = Math.ceil(BattleEnemy[j].health);
                            player.point += Math.floor(numShoot * BattleEnemy[j].bounty);
                        }
                        else {
                            player.point += Math.floor(BattleEnemy[j].health * BattleEnemy[j].bounty);
                            BattleEnemy[j].resource = 0;
                            BattleEnemy[j].health = 0;
                            removeEnemy.push(j);
                        }
                        Battle[i].onShoot = false;
                        Battle[i].assign_shoot(Battle[i]);

                    }
                }
                if (rangeShoot <= BattleEnemy[j].range) {
                    if (BattleEnemy[j].onShoot && BattleEnemy[j].health >= 0.1 && Battle[i].health >= 0.1) {
                        numShoot = Battle[i].shooting(BattleEnemy[j]);
                        if (Battle[i].health - numShoot >= 0.1) {
                            Battle[i].resource = Math.ceil(Battle[i].health);
                            Battle[i].health -= numShoot;
                        }
                        else {
                            Battle[i].resource = 0;
                            Battle[i].health = 0;
                            remove.push(i);
                        }
                        BattleEnemy[j].onShoot = false;
                        BattleEnemy[j].assign_shoot(BattleEnemy[j]);
                    }
                }
                Battle[i].healthText.text = (Battle[i].health.toFixed(0));
                BattleEnemy[j].healthText.text = (BattleEnemy[j].health.toFixed(0));
            }
        for (var i = 0; i < remove.length; i++) Battle.splice(remove.pop(), 1);
        for (var i = 0; i < removeEnemy.length; i++) BattleEnemy.splice(removeEnemy.pop(), 1);
    }

    function isCollision() {
        var colision;

        for (var i = 0; i < Battle.length; i++) {
            colision = true;
            for (var j = 0; j < BattleEnemy.length; j++)
                if (Battle[i].bitmap.x + 60 >= BattleEnemy[j].bitmap.x)
                    colision = false;
            if (colision) {
                Battle[i].positionX += Battle[i].mobility;
                Battle[i].bitmap.x = Battle[i].positionX;
                Battle[i].healthText.x = Battle[i].bitmap.x;
            }
        }

        for (var i = 0; i < BattleEnemy.length; i++) {
            colision = true;
            for (var j = 0; j < Battle.length; j++)
                if (BattleEnemy[i].bitmap.x <= Battle[j].bitmap.x + 60)
                    colision = false;
            if (colision) {
                BattleEnemy[i].positionX -= BattleEnemy[i].mobility;
                BattleEnemy[i].bitmap.x = BattleEnemy[i].positionX;
                BattleEnemy[i].healthText.x = BattleEnemy[i].bitmap.x;
            }
        }
    }

    function time() {
        timeCount += 1;
        if (timeCount == 60) {
            sec += 1;
            timeCount = 0;
        }
        if (sec == 60) {
            min += 1;
            sec = 0;
        }
        if (sec < 10) timeText.text = ("0" + min + " : 0" + sec);
        else timeText.text = ("0" + min + " : " + sec);
    }

    function showMessage(m) {
        msg = m;
        statusText.visible = true;
        statusText.text = (msg);
        setTimeout(function () { statusText.visible = false; }, 3000);
    }

    ////////////////////////////////////////////////
    function pointerUp(event) {
        if (newGame) {
            newGame = false;
        }
        else {
            if (!battle) {
                if (Battle.length != 0)
                    if (event.pageX >= 127 && event.pageX <= 278)
                        if (event.pageY >= 582 && event.pageY <= 699) {
                            battle = true;
                            for (var i = 0; i < Battle.length; i++) {
                                clearTimeout(Battle[i].myshoot);
                                Battle[i].assign_shoot(Battle[i]);
                                Battle[i].assign_health();
                            }
                            for (var i = 0; i < unitEnemy.length; i++) {
                                inBattleEnemy(Math.floor(Math.random() * 20) % 10, i);
                                if (i == 5) break;
                            }
                            for (var i = 0; i < BattleEnemy.length; i++) {
                                clearTimeout(BattleEnemy[i].myshoot);
                                BattleEnemy[i].assign_shoot(BattleEnemy[i]);
                                BattleEnemy[i].assign_health();
                            }
                            clearTimeout(greatBase.timeout);
                            timeText.visible = true;
                        }
                if (event.pageX >= 1081 && event.pageX <= 1232)
                    if (event.pageY >= 582 && event.pageY <= 699) {
                        greatBase.upgrade;
                    }
                if (Battle.length <= 6) { inBattle(); }
            }
        }
    }

    function inBattle() {
        if (event.pageX >= 232 && event.pageX <= 292) {
            if (event.pageY >= 125 && event.pageY <= 172)
                if (unit[0] != null) {
                    if (unit[0].choose) {
                        unit[0].choose = false;
                        Battle[count_click] = unit[0];
                        position = memPositionBattle[count_click].split(",");
                        unit[0].bitmap.x = position[0];
                        unit[0].bitmap.y = position[1];
                        ++count_click;
                    }
                }
        }
        if (event.pageX >= 232 && event.pageX <= 292) {
            if (event.pageY >= 179 && event.pageY <= 226)
                if (unit[1] != null) {
                    if (unit[1].choose) {
                        unit[1].choose = false;
                        Battle[count_click] = unit[1];
                        position = memPositionBattle[count_click].split(",");
                        unit[1].bitmap.x = position[0];
                        unit[1].bitmap.y = position[1];
                        ++count_click;
                    }
                }
        }
        if (event.pageX >= 232 && event.pageX <= 292) {
            if (event.pageY >= 233 && event.pageY <= 280)
                if (unit[2] != null) {
                    if (unit[2].choose) {
                        unit[2].choose = false;
                        Battle[count_click] = unit[2];
                        position = memPositionBattle[count_click].split(",");
                        unit[2].bitmap.x = position[0];
                        unit[2].bitmap.y = position[1];
                        ++count_click;
                    }
                }
        }
        if (event.pageX >= 232 && event.pageX <= 292) {
            if (event.pageY >= 287 && event.pageY <= 334)
                if (unit[3] != null) {
                    if (unit[3].choose) {
                        unit[3].choose = false;
                        Battle[count_click] = unit[3];
                        position = memPositionBattle[count_click].split(",");
                        unit[3].bitmap.x = position[0];
                        unit[3].bitmap.y = position[1];
                        ++count_click;
                    }
                }
        }
        if (event.pageX >= 232 && event.pageX <= 292) {
            if (event.pageY >= 341 && event.pageY <= 388)
                if (unit[4] != null) {
                    if (unit[4].choose) {
                        unit[4].choose = false;
                        Battle[count_click] = unit[4];
                        position = memPositionBattle[count_click].split(",");
                        unit[4].bitmap.x = position[0];
                        unit[4].bitmap.y = position[1];
                        ++count_click;
                    }
                }
        }
        if (event.pageX >= 232 && event.pageX <= 292) {
            if (event.pageY >= 395 && event.pageY <= 442)
                if (unit[5] != null) {
                    if (unit[5].choose) {
                        unit[5].choose = false;
                        Battle[count_click] = unit[5];
                        position = memPositionBattle[count_click].split(",");
                        unit[5].bitmap.x = position[0];
                        unit[5].bitmap.y = position[1];
                        ++count_click;
                    }
                }
        }
        if (event.pageX >= 232 && event.pageX <= 292) {
            if (event.pageY >= 449 && event.pageY <= 496)
                if (unit[6] != null) {
                    if (unit[6].choose) {
                        unit[6].choose = false;
                        Battle[count_click] = unit[6];
                        position = memPositionBattle[count_click].split(",");
                        unit[6].bitmap.x = position[0];
                        unit[6].bitmap.y = position[1];
                        ++count_click;
                    }
                }
        }
        if (event.pageX >= 232 && event.pageX <= 292) {
            if (event.pageY >= 503 && event.pageY <= 550)
                if (unit[7] != null) {
                    if (unit[7].choose) {
                        unit[7].choose = false;
                        Battle[count_click] = unit[7];
                        position = memPositionBattle[count_click].split(",");
                        unit[7].bitmap.x = position[0];
                        unit[7].bitmap.y = position[1];
                        ++count_click;
                    }
                }
        }
        if (event.pageX >= 158 && event.pageX <= 218) {
            if (event.pageY >= 125 && event.pageY <= 172)
                if (unit[8] != null) {
                    if (unit[8].choose) {
                        unit[8].choose = false;
                        Battle[count_click] = unit[8];
                        position = memPositionBattle[count_click].split(",");
                        unit[8].bitmap.x = position[0];
                        unit[8].bitmap.y = position[1];
                        ++count_click;
                    }
                }
        }
        if (event.pageX >= 158 && event.pageX <= 218) {
            if (event.pageY >= 179 && event.pageY <= 226)
                if (unit[9] != null) {
                    if (unit[9].choose) {
                        unit[9].choose = false;
                        Battle[count_click] = unit[9];
                        position = memPositionBattle[count_click].split(",");
                        unit[9].bitmap.x = position[0];
                        unit[9].bitmap.y = position[1];
                        ++count_click;
                    }
                }
        }
        if (event.pageX >= 158 && event.pageX <= 218) {
            if (event.pageY >= 233 && event.pageY <= 280)
                if (unit[10] != null) {
                    if (unit[10].choose) {
                        unit[10].choose = false;
                        Battle[count_click] = unit[10];
                        position = memPositionBattle[count_click].split(",");
                        unit[10].bitmap.x = position[0];
                        unit[10].bitmap.y = position[1];
                        ++count_click;
                    }
                }
        }
        if (event.pageX >= 158 && event.pageX <= 218) {
            if (event.pageY >= 287 && event.pageY <= 334)
                if (unit[11] != null) {
                    if (unit[11].choose) {
                        unit[11].choose = false;
                        Battle[count_click] = unit[11];
                        position = memPositionBattle[count_click].split(",");
                        unit[11].bitmap.x = position[0];
                        unit[11].bitmap.y = position[1];
                        ++count_click;
                    }
                }
        }
        if (event.pageX >= 158 && event.pageX <= 218) {
            if (event.pageY >= 341 && event.pageY <= 388)
                if (unit[12] != null) {
                    if (unit[12].choose) {
                        unit[12].choose = false;
                        Battle[count_click] = unit[12];
                        position = memPositionBattle[count_click].split(",");
                        unit[12].bitmap.x = position[0];
                        unit[12].bitmap.y = position[1];
                        ++count_click;
                    }
                }
        }
        if (event.pageX >= 158 && event.pageX <= 218) {
            if (event.pageY >= 395 && event.pageY <= 442)
                if (unit[13] != null) {
                    if (unit[13].choose) {
                        unit[13].choose = false;
                        Battle[count_click] = unit[13];
                        position = memPositionBattle[count_click].split(",");
                        unit[13].bitmap.x = position[0];
                        unit[13].bitmap.y = position[1];
                        ++count_click;
                    }
                }
        }
        if (event.pageX >= 158 && event.pageX <= 218) {
            if (event.pageY >= 449 && event.pageY <= 496)
                if (unit[14] != null) {
                    if (unit[14].choose) {
                        unit[14].choose = false;
                        Battle[count_click] = unit[14];
                        position = memPositionBattle[count_click].split(",");
                        unit[14].bitmap.x = position[0];
                        unit[14].bitmap.y = position[1];
                        ++count_click;
                    }
                }
        }
        if (event.pageX >= 158 && event.pageX <= 218) {
            if (event.pageY >= 503 && event.pageY <= 550)
                if (unit[15] != null) {
                    if (unit[15].choose) {
                        unit[15].choose = false;
                        Battle[count_click] = unit[15];
                        position = memPositionBattle[count_click].split(",");
                        unit[15].bitmap.x = position[0];
                        unit[15].bitmap.y = position[1];
                        ++count_click;
                    }
                }
        }

    }

    function inBattleEnemy(rand, i) {
        if (rand == 0) {
            if (unitEnemy[0] != null && unitEnemy[0].choose) {
                unitEnemy[0].choose = false;
                BattleEnemy[i] = unitEnemy[0];
                position = memPositionEnemyBattle[i].split(",");
                unitEnemy[0].bitmap.x = position[0];
                unitEnemy[0].bitmap.y = position[1];
            }
            else rand++;
        }
        if (rand == 1) {
            if (unitEnemy[1] != null && unitEnemy[1].choose) {
                unitEnemy[1].choose = false;
                BattleEnemy[i] = unitEnemy[1];
                position = memPositionEnemyBattle[i].split(",");
                unitEnemy[1].bitmap.x = position[0];
                unitEnemy[1].bitmap.y = position[1];
            }
            else rand++;
        }
        if (rand == 2) {
            if (unitEnemy[2] != null && unitEnemy[2].choose) {
                unitEnemy[2].choose = false;
                BattleEnemy[i] = unitEnemy[2];
                position = memPositionEnemyBattle[i].split(",");
                unitEnemy[2].bitmap.x = position[0];
                unitEnemy[2].bitmap.y = position[1];
            }
            else rand++;
        }
        if (rand == 3) {
            if (unitEnemy[3] != null && unitEnemy[3].choose) {
                unitEnemy[3].choose = false;
                BattleEnemy[i] = unitEnemy[3];
                position = memPositionEnemyBattle[i].split(",");
                unitEnemy[3].bitmap.x = position[0];
                unitEnemy[3].bitmap.y = position[1];
            }
            else rand++;
        }
        if (rand == 4) {
            if (unitEnemy[4] != null && unitEnemy[4].choose) {
                unitEnemy[4].choose = false;
                BattleEnemy[i] = unit[4];
                position = memPositionEnemyBattle[i].split(",");
                unitEnemy[4].bitmap.x = position[0];
                unitEnemy[4].bitmap.y = position[1];
            }
            else rand++;
        }
        if (rand == 5) {
            if (unitEnemy[5] != null && unitEnemy[5].choose) {
                unitEnemy[5].choose = false;
                BattleEnemy[i] = unitEnemy[5];
                position = memPositionEnemyBattle[i].split(",");
                unitEnemy[5].bitmap.x = position[0];
                unitEnemy[5].bitmap.y = position[1];
            }
            else rand++;
        }
        if (rand == 6) {
            if (unitEnemy[6] != null && unitEnemy[6].choose) {
                unitEnemy[6].choose = false;
                BattleEnemy[i] = unitEnemy[6];
                position = memPositionEnemyBattle[i].split(",");
                unitEnemy[6].bitmap.x = position[0];
                unitEnemy[6].bitmap.y = position[1];
            }
            else rand++;
        }
        if (rand == 7) {
            if (unitEnemy[7] != null && unitEnemy[7].choose) {
                unitEnemy[7].choose = false;
                BattleEnemy[i] = unitEnemy[7];
                position = memPositionEnemyBattle[i].split(",");
                unitEnemy[7].bitmap.x = position[0];
                unitEnemy[7].bitmap.y = position[1];
            }
            else rand++;
        }
        if (rand == 8) {
            if (unitEnemy[8] != null && unitEnemy[8].choose) {
                unitEnemy[8].choose = false;
                BattleEnemy[i] = unitEnemy[8];
                position = memPositionEnemyBattle[i].split(",");
                unitEnemy[8].bitmap.x = position[0];
                unitEnemy[8].bitmap.y = position[1];
            }
            else rand++;
        }
        if (rand == 9) {
            if (unitEnemy[9] != null && unitEnemy[9].choose) {
                unitEnemy[9].choose = false;
                BattleEnemy[i] = unitEnemy[9];
                position = memPositionEnemyBattle[i].split(",");
                unitEnemy[9].bitmap.x = position[0];
                unitEnemy[9].bitmap.y = position[1];
            }
            else rand++;
        }
        if (rand == 10) {
            if (unitEnemy[10] != null && unitEnemy[10].choose) {
                unitEnemy[10].choose = false;
                BattleEnemy[i] = unitEnemy[10];
                position = memPositionEnemyBattle[i].split(",");
                unitEnemy[10].bitmap.x = position[0];
                unitEnemy[10].bitmap.y = position[1];
            }
            else rand++;
        }
        if (rand == 11) {
            if (unitEnemy[11] != null && unitEnemy[11].choose) {
                unitEnemy[11].choose = false;
                BattleEnemy[i] = unitEnemy[11];
                position = memPositionEnemyBattle[i].split(",");
                unitEnemy[11].bitmap.x = position[0];
                unitEnemy[11].bitmap.y = position[1];
            }
            else rand++;
        }
        if (rand == 12) {
            if (unitEnemy[12] != null && unitEnemy[12].choose) {
                unitEnemy[12].choose = false;
                BattleEnemy[i] = unitEnemy[12];
                position = memPositionEnemyBattle[i].split(",");
                unitEnemy[12].bitmap.x = position[0];
                unitEnemy[12].bitmap.y = position[1];
            }
            else rand++;
        }
        if (rand == 13) {
            if (unitEnemy[13] != null && unitEnemy[13].choose) {
                unitEnemy[13].choose = false;
                BattleEnemy[i] = unitEnemy[13];
                position = memPositionEnemyBattle[i].split(",");
                unitEnemy[13].bitmap.x = position[0];
                unitEnemy[13].bitmap.y = position[1];
            }
            else rand++;
        }
        if (rand == 14) {
            if (unitEnemy[14] != null && unitEnemy[14].choose) {
                unitEnemy[14].choose = false;
                BattleEnemy[i] = unitEnemy[14];
                position = memPositionEnemyBattle[i].split(",");
                unitEnemy[14].bitmap.x = position[0];
                unitEnemy[14].bitmap.y = position[1];
            }
            else rand++;
        }
        if (rand == 15) {
            if (unitEnemy[15] != null && unitEnemy[15].choose) {
                unitEnemy[15].choose = false;
                BattleEnemy[i] = unitEnemy[15];
                position = memPositionEnemyBattle[i].split(",");
                unit[15].bitmap.x = position[0];
                unit[15].bitmap.y = position[1];
            }
            else inBattleEnemy(0, i);
        }
    }

})();




fighter.score.visible = true;
H.score.visible = true;
aircraft_carrier.score.visible = true;
tank.score.visible = true;
light_armo.score.visible = true;
hight_M_vehicle.score.visible = true;
light_tank.score.visible = true;
anti_aircraft_tank.score.visible = true;
bomber_aircraft.score.visible = true;
submarine.score.visible = true;
soilder.score.visible = true;
assault_ship.score.visible = true;
player.ricescore.visible = true;
player.goldscore.visible = true;
player.oilscore.visible = true;
player.ironscore.visible = true;